package com.yangguang.controller;

import com.yangguang.pojo.SmallTopic;
import com.yangguang.service.ImageFileService;
import com.yangguang.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/imageFile")
@CrossOrigin
public class ImageFileController {

    @Autowired
    private ImageFileService service;


    @PostMapping
    public Result characterRecognition(MultipartFile image, String schoolId, String examId) {
        try {
            long l = System.currentTimeMillis();
            int lastIndexOf = image.getOriginalFilename().lastIndexOf(".");
            String lastDir = image.getOriginalFilename().substring(lastIndexOf + 1);

            Map<Integer, Boolean> integerBooleanMap = service.characterRecognition(image, lastDir, schoolId, examId);
            System.out.println(System.currentTimeMillis() - l);
            return new Result(true, "识别成功", integerBooleanMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "识别失败");
    }

    @PostMapping("/addTemplate")
    public Result addTemplate(@RequestBody SmallTopic smallTopic) {
        try {
            service.addTemplate(smallTopic);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(true, "添加成功");
    }
}
